/**
 * 地址详情 识别解析地址
 */

import {getAddressListByLoacl} from '@dom/address'
/**
 * 解析
 * @param address 任意地址字符串
 * @returns {{name: string, mobile: string, detail: string, zip_code: string, phone: string}}
 */
const parse=async (address)=>{
  address = address || '';
  const parse = {
    name: '',
    detail: '',
    phone: ''
  };


  //去除空格...
  /*   address = address.replace(/\r\n/g, ' ').replace(/\n/g, ' ').replace(/\t/g, ' ');
  address = address.replace(/\s+/g, ""); */
  //自定义去除关键字，可自行添加
  const search = ['地址', '收货地址', '收货人', '收件人', '收货', '邮编', '电话', '：', ':', '；', ';', '，', ',', '。',' '];
  search.forEach(str => {
    address = address.replace(new RegExp(str, 'g'), ' ')
  });
  //多个空格replace为一个
  address = address.replace(/ {2,}/g, ' ');
  //整理电话格式
  address = address.replace(/(\d{3})-(\d{4})-(\d{4})/g, '$1$2$3');

  address = address.replace(/(\d{3}) (\d{4}) (\d{4})/g, '$1$2$3');

  const mobileReg = /(86-[1][0-9]{10})|(86[1][0-9]{10})|([1][0-9]{10})/g;
  const mobile = mobileReg.exec(address);
  if (mobile) {
    parse.phone = mobile[0];
    address = address.replace(mobile[0], ' ')
  }
  address = address.replace(/ {2,}/, ' ');


  let detail = await detail_parse_forward(address.trim());
  
  parse.province = detail.province;
  parse.city = detail.city;
  parse.area = detail.area;
  parse.detail = detail.detail;
  parse.contact = detail.name;
  
  return parse;
}
/**
 * 反向解析模式
 * 从后向前按  city province 逐级筛选
 * 有city的值即可说明解析成功
 */
const parse_province_by_city = (address,defaultData)=>{
  let data = defaultData.find(obj=>{
    let province = obj.childrens.find(city=>address.indexOf(city.name)>-1)
    if(province){
      return obj
    }
  })
  return data
}
/**
 * 正向解析模式
 * 从前到后按 province city area detail 逐级筛选
 * 有city的值即可说明解析成功
 * 此模式对地址顺序有要求
 * @param address
 * @returns {{province: string, city: string, area: string, detail: string, name:string}}
 */
const detail_parse_forward=async (address)=>{
  const parse = {
    province: '',
    city: '',
    area: '',
    detail: '',
    name: '',
  };

  let defaultData = await getAddressListByLoacl()

  // 识别出地址所在省份，城市，区域
  let province = defaultData.find(province=>{
    let index = address.indexOf(province.name);
    return index>-1
  })
  // 如果没有解析出省份，尝试用市数据，解析出省份
  if(!province){
    province = parse_province_by_city(address,defaultData)
    address = province.name+address
  }
  console.log(province)
  if(province){
    let index = address.indexOf(province.name)
    console.log(index)
    // index>0 表示名称在地址前面
    if(index>0){
      parse.name = address.substr(0, index).trim()
    }else{
      address = address.split(' ')
      if(address.length>1){
        parse.name = address[1]
      }
      address = address[0]
    }

    // 解析出省份
    parse.province = province.name
    address = address.substr(index + province.name.length)
    // 解析出省份对应下的城市
    let city = province.childrens.find(ct=>{
      let index = address.indexOf(ct.name);
      return index>-1
    })
    if(city){
      parse.city = city.name
      address = address.substr(city.name.length)

      // 解析出城市对应下的区域
      let area = city.childrens.find(area=>{
        let index = address.indexOf(area.name);
        return index>-1
      })
      if(area){
        parse.area = area.name
        address = address.substr(area.name.length)
      }
    }
  }
  parse.detail = address
  console.log(parse)
  return parse;
}

export default parse